﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Boshii.Map.DAL.Common;
using BoShii.FrameWork;
using Boshii.Map.Model;
using System.Data.OracleClient;
using System.Data;

namespace Boshii.Map.DAL.POI
{
    public class PoiDal:DalBase
    {
        public void GetAllMyPOI(PoiDs ds)
        {
            using (OracleHelper orahp = new OracleHelper())
            {
                string sql = string.Format(@"
                    Select {0} from {1}.T_POI A",
                    CreateFieldNames(ds.T_POI, "A"),
                    this.dbOwner);

                OracleCommand cmd = new OracleCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;

                orahp.Open();
                orahp.FillSchema(cmd, ds.T_POI);
                orahp.Execute(cmd, ds.T_POI);

            }
        }


        public void GetMyPOI(PoiDs ds, int POI_ID)
        {
            using (OracleHelper orahp = new OracleHelper())
            {
                string sql = string.Format(@"
					select
						{0}
						From {1}.T_POI A
						Where  POI_ID={2}",
                        CreateFieldNames(ds.T_POI, "A"),
                        this.dbOwner,
                        POI_ID);

                OracleCommand cmd = new OracleCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;

                orahp.Open();
                orahp.FillSchema(cmd, ds.T_POI);
                orahp.Execute(cmd, ds.T_POI);
            }
        }


        public void GetMyPOIByWhere(PoiDs ds, string where)
        {
            using (OracleHelper orahp = new OracleHelper())
            {
                string sql = string.Format(@"
					select
						{0}
						From {1}.T_POI A
						{2}",
                        CreateFieldNames(ds.T_POI, "A"),
                        this.dbOwner,
                        where);

                OracleCommand cmd = new OracleCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;

                orahp.Open();
                orahp.FillSchema(cmd, ds.T_POI);
                orahp.Execute(cmd, ds.T_POI);
            }
        }


        public void GetMyPOIByWhere(PoiDs ds, string where, string orderBy, int pageIndex, int pageSize)
        {
            using (OracleHelper orahp = new OracleHelper())
            {
                string sql = string.Format(@"
                    Select {0} 
                    From {1}.T_POI A {2} {3}",
                    CreateFieldNames(ds.T_POI, "A"),
                    this.dbOwner,
                    where,
                    orderBy.Equals(string.Empty) ? "" : "Order By " + orderBy);

                orahp.Open();
                orahp.ExecuteSP_Page(ds.T_POI, "pack_page.proc_page", sql, pageIndex, pageSize, true);
            }
        }


        public int GetMyPOICountByWhere(string where)
        {
            using (OracleHelper orahp = new OracleHelper())
            {
                string sql = string.Format(@"
					Select
						Count(POI_ID)
					From {0}.T_POI
					{1}",
                    this.dbOwner,
                    where);

                OracleCommand cmd = new OracleCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;

                orahp.Open();
                int recordCount;
                Int32.TryParse(orahp.ExecuteScalar(cmd).ToString(), out recordCount);
                return recordCount;
            }
        }


        public int UpdateMyPOI(PoiDs ds)
        {
            int updateRowCount = 0;
            using (OracleHelper orahp = new OracleHelper())
            {
                orahp.Open();
                orahp.BeginTransaction();
                try
                {
                    using (OracleCommand cmd = new OracleCommand())
                    {
                        #region SQL
                        string sql = string.Format(@"
                            Select {0} from {1}.T_POI A",
                             CreateFieldNames(ds.T_POI, "A"),
                             this.dbOwner);

                        #endregion
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = sql;
                        updateRowCount = orahp.AutoUpdate(cmd, ds.T_POI);
                    }
                    orahp.Commit();
                }
                catch (DBConcurrencyException dbcEx)
                {
                    orahp.Rollback();
                    throw new DBConcurrencyException("该记录已经被修改,请重新刷新页面", dbcEx);
                }
                catch (Exception ex)
                {
                    orahp.Rollback();
                    throw new Exception("服务器异常", ex);
                }
            }
            return updateRowCount;
        }
    }
}
